const { number } = require("yargs");
const { BaseCommand } = require("./base");

class StoreOptionalCommand extends BaseCommand {
    async execute() {
        const data = this.selfData;
        for (const k in data) {
            const presentVal = this.shareData[k];
            if (presentVal == null) {
                this.shareData[k] = data[k];
            } else {
                const expected = typeof data[k];
                const present = typeof presentVal;
                if (expected !== present) {
                    if (expected === "number") {
                        this.shareData[k] = parseFloat(presentVal);
                    } else if (expected === "boolean") {
                        if (
                            presentVal &&
                            presentVal !== "false" &&
                            presentVal !== "0"
                        ) {
                            this.shareData[k] = true;
                        } else {
                            this.shareData[k] = false;
                        }
                    }
                }
            }
        }
        return data;
    }
}

module.exports = {
    StoreOptionalCommand,
};
